<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加求购信息</title>
    <link rel="stylesheet" href="layui/css/layui.css" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" href="fonts/iconfonts.css" th:href="@{/fonts/iconfonts.css}">
    <script type="text/javascript" src="js/jquery.min.js" th:src="@{/js/jquery.min.js}"></script>
    <script type="text/javascript" src="layui/layui.js" th:src="@{/layui/layui.js}"></script>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px">
    <legend>添加求购信息</legend>
</fieldset>
<form class="layui-form" id="seekForm" >
<!--  标题-->
    <div class="layui-form-item" >
        <label class="layui-form-label">标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" id="title" lay-verify="required"  placeholder="请输入标题" class="layui-input" style="width:270px;">
        </div>
    </div>
<!-- 价格-->
    <div class="layui-form-item">
        <label class="layui-form-label">价格</label>
        <div class="layui-input-block">
            <input type="text" name="minPrice" id="minPrice" class="layui-input" lay-verify="require|number|compare" style="width: 100px;display: inline;text-align: center;" placeholder="最低价格"/> ~
            <input type="text" name="maxPrice" id="maxPrice" class="layui-input" lay-verify="require|number" style="width: 100px;display: inline;text-align: center;" placeholder="最高价格"/> 元
            <p id="errorMsg"></p>
        </div>
    </div>
<!-- 备注 -->
    <div class="layui-form-item" >
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
                <textarea cols="20" rows="5" style="width:270px;" name="remark" class="layui-textarea" lay-verify="required" placeholder="在这里详细描述下你发布的商品"></textarea>
        </div>
    </div>
    <!-- 提交 -->
    <div class="layui-form-item" style="text-align: center;margin-top: 20px;">
        <button id="submitBtn" class="layui-btn layui-btn-normal layui-btn-radius" lay-submit lay-filter="checkForm" style="width: 270px;margin: 20px 50px;">
            立即提交
        </button>
    </div>
</form>

<script>

    layui.use('form',function(){
        var form = layui.form;

    // 验证表单
        form.verify({
            'compare':function(value){
                let maxPrice = $("#maxPrice").val();
                if(parseFloat(value) > parseFloat(maxPrice)){
                    return "最低价格不能大于最高价格啊！";
                }
            }
        })

    // 提交添加求购信息
        form.on('submit(checkForm)',function(data){
            var formData = new FormData($("#seekForm")[0]);

            // 提交表单，将提交按钮禁用
            $('#submitBtn').addClass("layui-btn-disabled").attr("disabled",true);
            $.ajax({
                type: 'post',
                url: '/seek/addSeek',
                data: formData,
                contentType: false, //必须false才会自动加上正确的Content-Type,否则服务器无法识别
                processData: false,//注意以上两个配置，不加报错：Uncaught TypeError: Illegal invocation
                dataType: 'json',
                success: function(result){
                    if(result.success){
                        layer.msg(
                            result.message,
                            {
                                icon:1,
                                time: 1000
                            },
                            function(){
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            }
                        );
                    }else{
                        // 恢复提交按钮
                        $('#submitBtn').removeClass("layui-btn-disabled").attr("disabled",false);
                        layer.msg(
                            result.message,
                            {
                                icon:2,
                                time:1000
                            }
                        );
                    }
                },
                error: function(){
                    // 恢复提交按钮
                    $('#submitBtn').removeClass("layui-btn-disabled").attr("disabled",false);
                    layer.msg(
                        "网络错误",
                        {
                            icon:2,
                            time:2000
                        }
                    );
                }
            });
            return false;
        });

    });
</script>


</body>
</html>